package com.np.adapterdemo1;

import androidx.appcompat.app.AlertDialog;

import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseAdapterActivity extends ListActivity {

    private static final String TAG = BaseAdapterActivity.class.getSimpleName();

    private List<Map<String, Object>> mData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mData = getData();
        XAdapter adapter = new XAdapter(this);
        setListAdapter(adapter);
    }

    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("title", "华为");
        map.put("info", "遥遥领先！");
        map.put("img", R.drawable.ic_launcher_background);
        list.add(map);

        map = new HashMap<>();
        map.put("title", "苹果");
        map.put("info", "Make America Great Again!");
        map.put("img", R.drawable.ic_launcher_background);
        list.add(map);

        map = new HashMap<>();
        map.put("title", "三星");
        map.put("info", "电池绝对不会爆炸思密达！");
        map.put("img", R.drawable.ic_launcher_background);
        list.add(map);

        return list;
    }

    public final class ViewHolder {
        public ImageView img;
        public TextView title;
        public TextView info;
        public Button detail;
        public Button delete;
    }

    public class XAdapter extends BaseAdapter {

        private LayoutInflater mInflater;

        public XAdapter(Context context) {
            this.mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;

            if (convertView == null) {
                holder = new ViewHolder();

                convertView = mInflater.inflate(R.layout.activity_base_adapter, null);
                holder.img = convertView.findViewById(R.id.img);
                holder.title = convertView.findViewById(R.id.title);
                holder.info = convertView.findViewById(R.id.info);
                holder.detail = convertView.findViewById(R.id.button_detail);
                holder.delete = convertView.findViewById(R.id.button_delete);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.img.setBackgroundResource((Integer) mData.get(position).get("img"));
            holder.title.setText((String) mData.get(position).get("title"));
            holder.info.setText((String) mData.get(position).get("info"));

            holder.detail.setOnClickListener(v -> {
                new AlertDialog.Builder(BaseAdapterActivity.this)
                        .setTitle((String)mData.get(position).get("title"))
                        .setMessage((String) mData.get(position).get("info"))
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        })
                        .show();
            });

            holder.delete.setOnClickListener(v -> {
                Log.i(TAG, "position: " + position);
                mData.remove(position);
                notifyDataSetChanged(); // 这样才能刷新界面
            });

            return convertView;
        }
    }
}